clear*
set maxvar 11000

global date _2025_10_28
cd "/Users/atri0032/Dropbox/World Bank/Temperature, poverty, and inequality/Estimations/"

************************************************************************************************************************************
********************************** Figure S5: Nonlinear effects of temperature on poverty and inequality – Country-level analysis
************************************************************************************************************************************

    // --- POOR 2.15 ---
    use "${date}/_data/spid_for_analysis_v2.dta", clear
		
	local temp_bin bin3_1 bin3_2 bin3_3 bin3_4 bin3_5 bin3_6 bin3_7  bin3_9 bin3_10 bin3_11 bin3_12 bin3_13
	local controls ln_GDPppp Unempilo agri manuf popgr 

	collapse (mean) poor215 poor365 poor685 gini theil temp rainfall_sum humid ln_GDPppp Unempilo agri manuf edupri eduter popgr tech `temp_bin', by(code year)
	
    reghdfe poor215 `temp_bin' rainfall_sum humid `controls', absorb(code year) cluster(code) keepsingle	

	forval i=1(1)13 {
	cap gen estimate`i' = _b[bin3_`i']
	cap gen lb`i' = _b[bin3_`i'] - invttail(e(df_r),0.025)*_se[bin3_`i']
	cap gen ub`i' = _b[bin3_`i'] + invttail(e(df_r),0.025)*_se[bin3_`i']
	}

	gen estimate8=0
	gen lb8=0
	gen ub8=0

    gen n = _n
    keep if n == 1
    reshape long estimate lb ub, i(n) j(index)
    keep estimate lb ub index
    gen poor = 1

    tempfile poor215
    save `poor215'

    // --- Gini ---
    use "${date}/_data/spid_for_analysis_v2.dta", clear	
		
	local temp_bin bin3_1 bin3_2 bin3_3 bin3_4 bin3_5 bin3_6 bin3_7  bin3_9 bin3_10 bin3_11 bin3_12 bin3_13
	local controls ln_GDPppp Unempilo agri manuf popgr 

	collapse (mean) poor215 poor365 poor685 gini theil temp rainfall_sum humid ln_GDPppp Unempilo agri manuf edupri eduter popgr tech `temp_bin', by(code year)
	
    reghdfe gini `temp_bin' rainfall_sum humid `controls', absorb(code year) cluster(code) keepsingle	

	forval i=1(1)13 {
	cap gen estimate`i' = _b[bin3_`i']
	cap gen lb`i' = _b[bin3_`i'] - invttail(e(df_r),0.025)*_se[bin3_`i']
	cap gen ub`i' = _b[bin3_`i'] + invttail(e(df_r),0.025)*_se[bin3_`i']
	}

	gen estimate8=0
	gen lb8=0
	gen ub8=0

    gen n = _n
    keep if n == 1
    reshape long estimate lb ub, i(n) j(index)
    keep estimate lb ub index
    gen poor = 2

    tempfile gini
    save `gini'	

    // --- Combine and plot ---
    use `poor215', clear
    append using `gini'

    replace index = index + 0.15 if poor == 2

    graph twoway ///
        (rcap lb ub index if poor == 1, pstyle(ci) lcolor(navy%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 1, mcolor(navy%80)) ///
        (rcap lb ub index if poor == 2, pstyle(ci) lcolor(maroon%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 2, mcolor(maroon%80)) ///	
        , scheme(white_tableau) ///
        xtitle("Temperature bin") ///
        xlabel(1 "<0" 2 "[0,3)" 3 "[3,6)" 4 "[6,9)" ///
	5 "[9,12)" 6 "[12,15)" 7 "[15,18)" 8 "[18,21)" 9 "[21,24)" 10 "[24,27)" 11 "[27,30)" ///
	12 "[30,33)" 13 "33+", nogrid) ///
        ylabel(, nogrid) ///
        ytitle("Effects of temperature")  ///
        yline(0, lcolor(black) lpattern(solid) lwidth(thin)) ///
        legend(row(1) order(1 "Headcount poverty" 3 "Inequality") pos(6)) name("gr1", replace)

    graph export "${date}/_figures/_fig_s5.pdf", as(pdf) replace	
	
